package mattypserver.dyndns.org.oah;

import java.util.HashMap;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import android.util.Log;

public abstract class AbstractOAuthHelper implements OAuthHelper {
	
	public static final String TAG = "AbstractOAuthHelper";
	
	private OAuthConsumer consumer;
	private OAuthProvider provider;
	private OAuthRequestToken rToken;
	private OAuthAccessToken aToken;
	private OAuthVerifier verifier;
	private String callbackUrl;
	
	@Override
	public OAuthConsumer getOAuthConsumer() {
		return consumer;
	}

	@Override
	public void setOAuthConsumer(OAuthConsumer con) {
		consumer = con;
	}

	@Override
	public OAuthProvider getOAuthProvider() {
		return provider;
	}

	@Override
	public void setOAuthProvider(OAuthProvider prov) {
		provider = prov;
	}

	@Override
	public OAuthRequestToken getRequestToken() {
		return rToken;
	}

	@Override
	public void setRequestToken(OAuthRequestToken rt) {
		rToken = rt;
	}

	@Override
	public OAuthVerifier getVerifier() {
		return verifier;
	}

	@Override
	public void setOAuthVerifier(OAuthVerifier av) {
		verifier = av;

	}

	@Override
	public OAuthAccessToken getAccessToken() {
		return aToken;
	}

	@Override
	public void setAccessToken(OAuthAccessToken at) {
		aToken = at;

	}
	
	@Override
	public void setCallBackUrl(String callbackUrl) {
		this.callbackUrl = callbackUrl;
	}
	
	@Override
	public String getCallBackUrl() {
		return callbackUrl;
	}
	
	public String makeAuthenticatedRequest(String url, RequestMethod method, HashMap<String, String> params){
		Log.i(TAG, "In makeAuthenticatedRequest");
		try{
			OAuthHttpClient client = new OAuthHttpClient(url, this);
			if(params != null && !params.isEmpty()){
				for(String key : params.keySet()){
					client.addParam(key, params.get(key));
				}
			}
			Log.d(TAG, "url: [" + url + "]");
			client.execute(method);
			return client.getResponse();
		}catch(Exception e){
			Log.e(TAG, e.toString());
			return null;
		}
	}

}
